package com.piece.core.web.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.piece.core.framework.constant.ParamConstants;
import com.piece.core.framework.enums.WebFilterOrder;
import com.piece.core.web.convert.FastJsonHttpDateConverter;
import com.piece.core.web.filter.TenantFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import java.util.ArrayList;
import java.util.List;

/**
 * 默认SpringMVC拦截器
 */
@Slf4j
public class WebMvcAutoConfig {

    @Value("${spring.messages.basename}")
    private String basenames;

    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpDateConverter fastConverter = new FastJsonHttpDateConverter();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue                             //输出值为null的字段
                //SerializerFeature.PrettyFormat,                               //日期格式转换
                //SerializerFeature.WriteNullStringAsEmpty,                     //字符类型字段如果为null,输出为”“,而非null
                //SerializerFeature.WriteNullListAsEmpty,                       //字符类型字段如果为null,输出为”“,而非null
                //SerializerFeature.DisableCircularReferenceDetect              //消除对同一对象循环引用的问题
        );
        fastConverter.setFastJsonConfig(fastJsonConfig);

        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        supportedMediaTypes.add(MediaType.APPLICATION_PDF);
        supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XML);
        supportedMediaTypes.add(MediaType.IMAGE_GIF);
        supportedMediaTypes.add(MediaType.IMAGE_JPEG);
        supportedMediaTypes.add(MediaType.IMAGE_PNG);
        supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
        supportedMediaTypes.add(MediaType.TEXT_XML);
        fastConverter.setSupportedMediaTypes(supportedMediaTypes);

        return fastConverter;
    }

//    @Bean
//    public Jackson2ObjectMapperBuilderCustomizer dateTimeConverter() {
//        return builder -> {
//            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS)));
//            builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS)));
//        };
//    }

    public ResourceBundleMessageSource resourceBundleMessageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        if (null != basenames) {
            messageSource.setBasenames(basenames.split(","));
        }
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setUseCodeAsDefaultMessage(true);
        return messageSource;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        // 参数名
        localeChangeInterceptor.setParamName(ParamConstants.LANGUAGE);
        return localeChangeInterceptor;
    }

//    @Bean
//    public LocaleResolver localeResolver() {
//        CookieLocaleResolver localeResolver = new CookieLocaleResolver();
//        localeResolver.setCookieName(CookieConstants.LANGUAGE);
//        localeResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
//        localeResolver.setCookieMaxAge(3600);
//        return localeResolver;
//    }

    @Bean
    public FilterRegistrationBean<TenantFilter> tenantContextWebFilter() {
        FilterRegistrationBean<TenantFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new TenantFilter());
        registrationBean.setOrder(WebFilterOrder.TENANT_FILTER);
        return registrationBean;
    }
}
